using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Facebook.Entity;

namespace DevicePhotoSample
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //connect to facebook
            facebookService1.ApplicationKey = "8141812bae6d6912fa098a804f53389e";
            facebookService1.Secret = "3bcc261c1ffc6c549b7da1e1b79b9f46";
            facebookService1.ConnectToFacebook();
            
            RefreshAlbumList();

        }

        private void Form1_Closing(object sender, CancelEventArgs e)
        {
            facebookService1.LogOff();
        }

        private void lvAlbums_ItemActivate(object sender, EventArgs e)
        {
            int selectedindex = lvAlbums.SelectedIndices[0];
            if (selectedindex > -1)
            {
                Album a = (Album)lvAlbums.Items[selectedindex].Tag;
                AlbumForm af = new AlbumForm(this, a);
                //af.Parent = this;
                af.ShowDialog();
            }
        }

        private void mnuNewAlbum_Click(object sender, EventArgs e)
        {
            AlbumPropertiesForm apf = new AlbumPropertiesForm(this, null);
            apf.ShowDialog();
            RefreshAlbumList();
        }

        private void mnuProperties_Click(object sender, EventArgs e)
        {
            if (lvAlbums.SelectedIndices.Count > 0 && lvAlbums.SelectedIndices[0] > -1)
            {
                int index = lvAlbums.SelectedIndices[0];
                ListViewItem lvi = lvAlbums.Items[index];
                Album a = (Album)lvi.Tag;
                AlbumPropertiesForm apf = new AlbumPropertiesForm(this, a);
                apf.ShowDialog();
            }
        }

        private void RefreshAlbumList()
        {
            lvAlbums.Items.Clear();
            ilThumbs.Images.Clear();
            ilThumbs.Images.Add((Image)DevicePhotoSample.Properties.Resources.ResourceManager.GetObject("empty"));
            Cursor.Current = Cursors.WaitCursor;
            lvAlbums.BeginUpdate();

            Collection<Album> albums = facebookService1.GetPhotoAlbums();
            Collection<string> pid = new Collection<string>();

            int i = 1;

            foreach (Album a in albums)
            {
                ListViewItem lvi = new ListViewItem(a.Name);
                lvi.Tag = a;

                //check for a missing album image and put generic placeholder
                if (string.IsNullOrEmpty(a.CoverPhotoId) || a.CoverPhotoId == "0")
                {
                    lvi.ImageIndex = 0;
                }
                else
                {
                    lvi.ImageIndex = i;
                    i++;
                }
                
                lvAlbums.Items.Add(lvi);

                pid.Add(a.CoverPhotoId);

                
            }

            Collection<Photo> photos = facebookService1.GetPhotos(pid);

            foreach (Photo p in photos)
            {
                ilThumbs.Images.Add(p.PictureSmall);
            }

            lvAlbums.EndUpdate();

            Cursor.Current = Cursors.Default;
        }
    }
}